package com.spc.car.web.controller.adm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spc.car.common.utils.StringUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.domain.adm.po.AdmVehicleInfoPO;
import com.spc.car.data.domain.adm.vo.AdmVehicleInfoVO;
import com.spc.car.data.entity.adm.AdmVehicleInfo;
import com.spc.car.data.service.adm.AdmVehicleInfoService;
import com.spc.car.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "车辆信息(公司车辆管理)")
@Slf4j
@Validated
@RestController
@RequestMapping("/admVehicle")
public class VehicleInfoController extends BaseController {

    @Autowired
    AdmVehicleInfoService admVehicleInfoService;


    @ApiOperation(value = "保存车辆信息", notes = "保存车辆相关信息")
    @PostMapping("/saveVehicle")
    public AjaxResult saveVehicle(@Valid @RequestBody AdmVehicleInfoVO admVehicleInfoVO) {
        boolean result = admVehicleInfoService.addOrUpdateVehicle(admVehicleInfoVO);
        return result? AjaxResult.success("保存成功！") : AjaxResult.error("保存失败！");
    }

    @ApiOperation(value = "获取车辆信息", notes = "获取车辆信息")
    @GetMapping("/getVehicle")
    @ApiImplicitParam(value = "ID", name = "id", required = true, type = "Long")
    public AjaxResult getVehicle(Long id) {
        AdmVehicleInfo admVehicleInfo = admVehicleInfoService.getVehicleById(id);
        return AjaxResult.success("获取成功！", admVehicleInfo);
    }

    @ApiOperation(value = "车辆信息列表", notes = "车辆信息列表")
    @PostMapping("/listVehicle")
    public AjaxResult listVehicle(@RequestBody AdmVehicleInfoPO admVehicleInfoPO) {
        IPage page = admVehicleInfoService.listForPage(admVehicleInfoPO);
        return AjaxResult.success("查询成功！", page);
    }

    @ApiOperation(value = "删除车辆信息", notes = "删除车辆信息")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam(name = "id", required = true) Long id) {
        admVehicleInfoService.removeById(id);
        return AjaxResult.success("删除成功！");
    }

    @ApiOperation(value = "下拉获取车辆信息", notes = "下拉获取车辆信息")
    @GetMapping("/listSelectVehicleInfo")
    public AjaxResult listSelectVehicleInfo(@RequestParam(name = "vehicleName", required = false) String vehicleName) {

        List<AdmVehicleInfo> listSelect =
                admVehicleInfoService.list(new LambdaQueryWrapper<AdmVehicleInfo>()
                        .eq(StringUtils.isNotBlank(vehicleName), AdmVehicleInfo::getCarName, vehicleName)
                        .eq(AdmVehicleInfo::getCarUseStatus, "01")
                        .orderByDesc(AdmVehicleInfo::getCreateTime));

        return AjaxResult.success("获取成功！", listSelect);
    }
}
